/**
 * @param {string} JSON  {"周一":{"range0":[],"range1":[],"range2":null}}
 * @returns {object}
 */
export function rangeJsonToObj(json) {
  const obj = [
    {
      title: '周一',
      range0: null,
      range1: null,
      range2: null,
      range3: null,
      range4: null,
      range5: null,
      range6: null,
      range7: null,
    },
    {
      title: '周二',
      range0: null,
      range1: null,
      range2: null,
      range3: null,
      range4: null,
      range5: null,
      range6: null,
      range7: null,
    },
    {
      title: '周三',
      range0: null,
      range1: null,
      range2: null,
      range3: null,
      range4: null,
      range5: null,
      range6: null,
      range7: null,
    },
    {
      title: '周四',
      range0: null,
      range1: null,
      range2: null,
      range3: null,
      range4: null,
      range5: null,
      range6: null,
      range7: null,
    },
    {
      title: '周五',
      range0: null,
      range1: null,
      range2: null,
      range3: null,
      range4: null,
      range5: null,
      range6: null,
      range7: null,
    },
    {
      title: '周六',
      range0: null,
      range1: null,
      range2: null,
      range3: null,
      range4: null,
      range5: null,
      range6: null,
      range7: null,
    },
    {
      title: '周日',
      range0: null,
      range1: null,
      range2: null,
      range3: null,
      range4: null,
      range5: null,
      range6: null,
      range7: null,
    },
  ]
  if (json) {
    const data = JSON.parse(json)
    obj.forEach((item) => {
      const tmp = data[item.title]
      Object.keys(tmp).forEach((element) => {
        if (tmp[element] !== null) item[element] = tmp[element]
      })
    })
  }
  return obj
}

/**
 * @param {Array} obj
 * @returns {String}
 *  // TODO 优化高大上的算法
 */
export function objToRangeJson(obj) {
  if (Array.isArray(obj)) {
    const tmp = {}
    obj.forEach((item) => {
      tmp[item.title] = {
        range0: item.range0 ? item.range0 : null,
        range1: item.range1 ? item.range1 : null,
        range2: item.range2 ? item.range2 : null,
        range3: item.range3 ? item.range3 : null,
        range4: item.range4 ? item.range4 : null,
        range5: item.range5 ? item.range5 : null,
        range6: item.range6 ? item.range6 : null,
        range7: item.range7 ? item.range7 : null,
      }
    })
    return JSON.stringify(tmp)
  } else {
    return null
  }
}
